package com.cc.dao.impl;

import com.cc.dao.AttendanceRecordDao;
import com.cc.dao.BaseDao;
import com.cc.entity.AttendanceRecord;
import com.cc.entity.Page;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @description:TODO
 * @authorAdministrator
 * @date 2025/3/31 11:53
 */
public class AttendanceRecordDaoImpl extends BaseDao implements AttendanceRecordDao {


    @Override
    public boolean isClockIn(String employeeId) {
        String sql = "SELECT count(1) FROM `attendance_records` WHERE employee_id = ? AND date = DATE_FORMAT(CURDATE(), '%Y-%m-%d')";
        ResultSet rs = null;
        try {
            rs = executeQuery(sql, employeeId);
            if (rs != null && rs.next()) {
                return rs.getInt(1) > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closedAll(rs);
        }
        return false;
    }

    @Override
    public void clockIn(String employeeId) {
        String sql = "INSERT INTO attendance_records (employee_id, check_in_time, `status`, date) " +
                "VALUES (?, TIME(NOW()), " +
                "CASE WHEN TIME(NOW()) > '09:00:00' THEN '迟到' ELSE '正常' END, " +
                "DATE_FORMAT(CURDATE(),'%Y-%m-%d'))";
        executeUpdate(sql, employeeId);
    }

    @Override
    public void updateClockIn(String employeeId) {
        String sql = "UPDATE attendance_records SET check_out_time = TIME(NOW()) " +
                "WHERE employee_id = ? AND date = DATE_FORMAT(CURDATE(),'%Y-%m-%d')";
        executeUpdate(sql, employeeId);
    }

    @Override
    public Page<AttendanceRecord> findAll(Page<AttendanceRecord> page){
        List<AttendanceRecord> list = new ArrayList<>();
        List<Object> params =new ArrayList<>();
        String sql = "SELECT id,DATE_FORMAT(date, '%Y-%m-%d') AS format_date, " +
                " ELT(DAYOFWEEK(date), '星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六') AS week_day, " +
                " TIME(check_in_time) AS in_time, " +
                " TIME(check_out_time) AS out_time, " +
                " TIMEDIFF(check_out_time, check_in_time) AS work_duration, " +
                " TIMESTAMPDIFF(MINUTE, TIME('08:00:00'), TIME(check_in_time)) AS shicha,status " +
                " FROM attendance_records ORDER BY date DESC LIMIT ?, ?";
        params.add((page.getPageNum() - 1) * page.getPageSize());
        params.add(page.getPageSize());
        ResultSet rs = executeQuery(sql, params.toArray());
        if (rs != null) {
            try {
                while (rs.next()) {
                    AttendanceRecord att = new AttendanceRecord();
                    att.setId(rs.getInt("id"));
                    att.setDate(rs.getDate("format_date"));
                    att.setXingqi(rs.getString("week_day"));
                    att.setCheckInTime(rs.getTime("in_time"));
                    att.setCheckOutTime(rs.getTime("out_time"));
                    att.setGongshi(rs.getTime("work_duration"));
                    att.setShicha(rs.getInt("shicha"));
                    att.setStatus(rs.getString("status"));
                    list.add(att);
                }
            } catch (SQLException e) {
               e.printStackTrace();
            } finally {
                closedAll(rs);
            }
        }
        //把结果集组装到我的分页中
        page.setData(list);
        page.setTotal(getTotal());
        return page;
    }

    @Override
    public int getTotal() {
        List<Object> params = new ArrayList<>();
        int total = 0;
        String sql = "SELECT COUNT(*) count FROM attendance_records";
        ResultSet rs = executeQuery(sql);
        if (rs != null) {
            try {
                while (rs.next()) {
                    total = rs.getInt("count");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                closedAll(rs);
            }
        }
        return total;
    }
}
